package com.neusoft.databus.sap.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.databus.agent.bean.PoolStatusBean;
import com.neusoft.databus.agent.pool.opc.OpcConnector;
import com.neusoft.databus.agent.pool.serial.SerialConnector;
import com.neusoft.databus.agent.pool.snmp.SnmpConnector;
import com.neusoft.databus.agent.pool.socket.SocketConnector;
import com.neusoft.databus.agent.pool.socket2.Socket2Connector;
import com.neusoft.databus.agent.service.SupportService;
import com.neusoft.databus.common.connection.OpcConnection;
import com.neusoft.databus.common.connection.SerialConnection;
import com.neusoft.databus.common.connection.SnmpConnection;
import com.neusoft.databus.common.connection.SocketConnection;

/**
 * 服务支撑控制器<br>
 * 提供运维接口
 *
 * @author sunchf
 * @date 2018年12月28日 上午10:13:58
 */

@RestController
@RequestMapping(value = "/api/w/support")
public class AgentSupportController {

    @Autowired
    private SupportService supportService;
    @Autowired
    private SocketConnector socketConnector;
    @Autowired
    private Socket2Connector socket2Connector;
    @Autowired
    private SnmpConnector snmpConnector;
    @Autowired
    private OpcConnector opcConnector;
    @Autowired
    private SerialConnector serialConnector;

    @GetMapping("/pools")
    public ResultListDTO<PoolStatusBean> intenalPools() {
        final List<PoolStatusBean> beans = this.supportService.realtimePoolsStatus();
        return ResultListDTO.success(beans);
    }

    @SuppressWarnings("deprecation")
    @DeleteMapping("/pools/{type}/{key}")
    public ResultDTO<Void> removePooledObjectInPools(@PathVariable final String type, @PathVariable final String key) {

        switch (type) {
        case "socket":
        case "socket2":
        case "snmp":
            final String[] tuple = key.split(":");
            if ("socket".equalsIgnoreCase(type)) {
                final SocketConnection connection = new SocketConnection();
                connection.setIpAddress(tuple[0]);
                connection.setPort(Integer.valueOf(tuple[1]));
                this.socketConnector.removeAllConns(connection);
            } else if ("socket2".equalsIgnoreCase(type)) {
                final SocketConnection connection = new SocketConnection();
                connection.setIpAddress(tuple[0]);
                connection.setPort(Integer.valueOf(tuple[1]));
                this.socket2Connector.removeAllConns(connection);
            } else if ("snmp".equalsIgnoreCase(type)) {
                final SnmpConnection connection = new SnmpConnection();
                connection.setIpAddress(tuple[0]);
                connection.setPort(Integer.valueOf(tuple[1]));
                connection.setCommunity(tuple[2]);
                this.snmpConnector.removeAllConns(connection);
            }
            break;

        case "serial":
            final SerialConnection connection = new SerialConnection();
            connection.setComValue(key);
            this.serialConnector.removeAllConns(connection);
            break;
        case "opc":
            final OpcConnection opcConnection = new OpcConnection();
            opcConnection.setHost(key);
            this.opcConnector.removeAllConns(opcConnection);
            break;
        default:
            break;
        }

        return ResultDTO.success();
    }

}
